<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <script>
    /* 
      请封装一个函数,可以按照以下调用的方式进行求和
        - add(1,2,3)
        - add(1,2)(3)
        - add(1)(2,3)
        - add(1)(2)(3)
        - add(1,2)(2,2)(3,2)....
    */

    function add(...arg) {
      let num = 0
      // 计算第一次传入的值
      num = arg.reduce((p, c) => p + c, 0)

      const temp = (...arg) => {
        num = arg.reduce((p, c) => p + c, num)
        return temp
      }

      temp.toString = function () {
        return num
      }
      
      return temp

    }

    alert(add(1, 2, 3))
    alert(add(1, 2)(3))
    alert(add(1)(2, 3))
    alert(add(1)(2)(3))
  </script>
</body>

</html>